using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.IO;

namespace PiratesOfBermuda
{
    class MainMenuScreen : MenuScreen
    {
        //Save State File Name
        public static string fileName = "MySave.dat";

        public MainMenuScreen()
            : base(String.Empty)
        {
            IsPopup = true;

            // Create our menu entries.
            MenuEntry singleGameMenuEntry = new MenuEntry("Single Player");
            MenuEntry multiGameMenuEntry = new MenuEntry("Multi Player");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");

            
            // Hook up menu event handlers.
            singleGameMenuEntry.Selected += SinglePlayerSelected;

            multiGameMenuEntry.Selected += StartMultiGameMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            MenuEntries.Add(singleGameMenuEntry);
            MenuEntries.Add(multiGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }
        void SinglePlayerSelected(object sender, EventArgs e)
        {
            ReloadLastGameState(false);
        }

        private void ReloadLastGameState(bool p)
        {
            using (IsolatedStorageFile isolatedStorageFile
                = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isolatedStorageFile.FileExists(fileName))
                {
                    if (System.Windows.MessageBox.Show("Old game available.\nDo you want to continue last game?", "Load Game", System.Windows.MessageBoxButton.OKCancel) == System.Windows.MessageBoxResult.OK)
                    {
                        using (IsolatedStorageFileStream fileStream
                            = isolatedStorageFile.OpenFile(fileName, FileMode.Open))
                        {
                            using (StreamReader streamReader = new StreamReader(fileStream))
                            {
                                string line;
                                while (!streamReader.EndOfStream)
                                {
                                    line = streamReader.ReadLine();
                                    string[] split = line.Split(',');

                                    switch (split[0])
                                    {
                                        case "Player":
                                            PhoneApplicationService.Current.State.Add(split[1] + "_" + split[2], "Player");
                                            PhoneApplicationService.Current.State.Add("PlayerLifeRemained", int.Parse(split[3]));
                                            break;
                                        case "Enemy":
                                            PhoneApplicationService.Current.State.Add(split[1] + "_" + split[2], "Enemy");
                                            break;
                                        case "Whirl":
                                            PhoneApplicationService.Current.State.Add(split[1] + "_" + split[2], "Whirl");
                                            break;
                                        case "Island":
                                            PhoneApplicationService.Current.State.Add(split[1] + "_" + split[2], "Island");
                                            break;
                                        case "Rubble":
                                            PhoneApplicationService.Current.State.Add(split[1] + "_" + split[2], "Rubble");
                                            break;
                                    }
                                }
                                PhoneApplicationService.Current.State.Add("IsReload", true);
                                streamReader.Close();
                            }
                        }
                    }
                    isolatedStorageFile.DeleteFile(fileName);
                }
                ScreenManager.AddScreen(new SinglePlayerScreen());
                this.ExitScreen();
            }
        }
        

        protected override void UpdateMenuEntryLocations()
        {
            base.UpdateMenuEntryLocations();

            foreach (var entry in MenuEntries)
            {
                var position = entry.Position;

                position.Y += 40;

                entry.Position = position;
            }
        }

        void StartMultiGameMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new MultiPlayersMenuScreen());
            ExitScreen();
        }

        protected override void OnCancel()
        {
            ScreenManager.Game.Exit();
        }
    }
}
